using System;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.Lua.RuntimeService.WowBench;
using NUnit.Framework;

namespace Microsoft.WowAddonStudio.Lua.RuntimeService.Tests.WowBench
{
	[TestFixture]
	public class WoWBenchOutputWriterTest
	{
		private RichTextBox textBox;
		private RichTextOutputControl outputControl;
		private WoWBenchOutputWriter writer;

		#region SetUp

		/// <summary>
		/// Sets up.
		/// </summary>
		[SetUp]
		public void SetUp()
		{
			if (textBox != null) textBox.Dispose();
			textBox = new RichTextBox();
			outputControl = new RichTextOutputControl(textBox);
			writer = new WoWBenchOutputWriter(outputControl);
		}

		#endregion

		#region TearDown

		/// <summary>
		/// Tears down.
		/// </summary>
		[TearDown]
		public void TearDown()
		{
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_SimpleWrite()
		{
			const string testText = "Test SimpleText.";
			writer.Write(testText);
			Assert.AreEqual(testText, outputControl.Text);
		}

		[Test(Description = "")]
		public void Test_SimpleWriteLine()
		{
			const string testText = "Test SimpleText.";
			writer.WriteLine(testText);
			Assert.AreEqual(testText + "\n", outputControl.Text);
		}

		[Test(Description = "")]
		public void Test_SimpleWriteErrorText()
		{
			const string testText = "Test ErrorText.";
			writer.WriteLine(testText);
			Assert.AreEqual(testText + "\n", outputControl.Text);
		}

		[Test(Description = "")]
		public void Test_SimpleWriteInfoText()
		{
			const string testText = "Test InfoText.";
			writer.WriteLine(testText);
			Assert.AreEqual(testText + "\n", outputControl.Text);
		}

		[Test(Description = "")]
		public void Test_SimpleWriteFormattedText()
		{
			const string testText = "Test SimpleText.\r\n";
			writer.WriteLineFormat(testText);
			Assert.AreEqual("Test SimpleText.\n\n", outputControl.Text);
		}

		[Test(Description = "")]
		public void Test_WriteFormattedText()
		{
			const string testText = "Test SimpleText.\r";
			writer.WriteLineFormat(testText);
			Assert.AreEqual("Test SimpleText.\n", outputControl.Text);
		}

		private int beforeWriteInvocationCount;
		private int afterWriteInvocationCount;

		[Test(Description = "")]
		public void Test_EventSubscription()
		{
			writer.OnAfterWrite += OnAfterWriteTest;
			writer.OnBeforeWrite += OnBeforeWriteTest;
			beforeWriteInvocationCount = 0;
			afterWriteInvocationCount = 0;
			writer.WriteFormat("Test SimpleText");
			Assert.AreEqual(1, beforeWriteInvocationCount);
			Assert.AreEqual(1, afterWriteInvocationCount);
			writer.UnsubscribeEvents();
			writer.WriteLineFormat("Test SimpleText");
			Assert.AreEqual(1, beforeWriteInvocationCount);
			Assert.AreEqual(1, afterWriteInvocationCount);
		}

		/// <summary>
		/// Called when [before write test].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="Microsoft.WowAddonStudio.Lua.Runtime.WoWBench.OnBeforeWriteEventArgs"/> instance containing the event data.</param>
		private void OnBeforeWriteTest(object sender, OnBeforeWriteEventArgs e)
		{
			Assert.IsNotNull(e);
			Assert.IsNotNull(e.Text);
			Console.WriteLine("OnAfterWrite event: {0}", e.Text);
			beforeWriteInvocationCount++;
		}

		/// <summary>
		/// Called when [after write test].
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="Microsoft.WowAddonStudio.Lua.Runtime.WoWBench.OutputWriterEventArgs"/> instance containing the event data.</param>
		private void OnAfterWriteTest(object sender, OutputWriterEventArgs e)
		{
			Assert.IsNotNull(e);
			Assert.IsNotNull(e.Text);
			Console.WriteLine("OnAfterWrite event: {0}", e.Text);
			afterWriteInvocationCount++;
		}

		#endregion
	}
}